package com.vanzo.some.appinfo;

import android.util.Log;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by tisen on 2017/10/27.
 */

public class Cn2Spell {

    private static Pattern p = Pattern.compile("[\u4e00-\u9fa5]");

    public static void main(String[] args) {

        char[] nameChar = "个人助理＋+".toCharArray();
        for (char c : nameChar) {
            System.out.println(p.matcher(String.valueOf(c)).matches() ? "true" : "false");
        }

    }

    public static final String TAG = "Cn2Spell";

    public static boolean isChinese(String chines) {
        return chines.toCharArray()[0] > 128 ? true : false;
    }


    public static String converterToSpell(String chines) {
        if (chines == null || chines.equals(""))
            return null;
        String pinyinString = "";
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : nameChar) {
            if (p.matcher(String.valueOf(c)).matches()) {
                try {
                    String s = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0];
                    if (s.equals(" ") || s.equals("")) {
                    } else {
                        pinyinString += s;
                    }
                } catch (Exception e) {
                    Log.d(TAG, "the error chines is " + chines);
                    Log.d(TAG, "the error char is " + c);
                    e.printStackTrace();
                }
            } else {
                pinyinString += c;
            }
        }
        return pinyinString;
    }

    public static String converterToFirstSpell(String chines) {
        String pinyinString = "";
        char[] nameChar = chines.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (char c : nameChar) {
            if (c > 128) {
                try {
                    String s = PinyinHelper.toHanyuPinyinStringArray(c, defaultFormat)[0];
                    if (s.equals(" ") || s.equals("")) {
                    } else {
                        pinyinString += s.charAt(0);
                    }
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
            } else {
                pinyinString += c;
            }

        }
        return pinyinString;

    }
}
